<?php

namespace Trawind\Basics\Traits;

use Trawind\Basics\Enums\SystemConstant;

trait HasEnumerate
{

    /**
     * @param  array     $enums
     * @param  int|null  $index
     * @return int|int[]|string|string[]
     */
    public static function enumerate(array $enums, ?int $index = null)
    {
        $enumsMap = array_flip($enums);
        return is_null($index) ? $enumsMap : $enumsMap[$index] ?? '';
    }


    /**
     * @param  array     $enums
     * @param  int|null  $index
     * @param  string    $module
     * @return array|bool|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Translation\Translator|string|null
     */
    public static function tranEnum(array $enums, ?int $index = null, string $module = '')
    {
        $enumsMap = static::enumerate($enums, $index);
        return is_array($enumsMap) ? __trans_list($enumsMap) : __trans($enumsMap, $module);
    }


    /**
     * @param  int|null  $statusTypeId
     * @param  string    $module
     * @return array|bool|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Translation\Translator|string|null
     */
    public static function tranStatus(?int $statusTypeId = null, string $module = '')
    {
        try {
            $status = constant('static::STATUS_ENUM');
        } catch (\Exception $e) {
            $status = SystemConstant::SYSTEM_STATUS_ENUM;
        }

        return static::tranEnum($status, $statusTypeId, $module);
    }
}